import TileLayer from 'ol/layer/Tile'
import { XYZ, WMTS } from 'ol/source'
import { mapUrl } from '@/store/map'
import TileGrid from 'ol/tilegrid/TileGrid'
import proj4 from 'proj4'
import { register } from 'ol/proj/proj4'
import { addProjection, get, addCoordinateTransforms, transform } from 'ol/proj'
import Projection from 'ol/proj/Projection.js'
import Tile from 'ol/layer/Tile'
import VectorLayer from 'ol/layer/Vector.js'
import VectorSource from 'ol/source/Vector.js'
import GeoJSON from 'ol/format/GeoJSON.js'
import { Stroke, Style, Circle, Fill } from 'ol/style'
import { GEOJSON_CONST } from '@/assets/data/resource.js'
import axios from 'axios'

const mapStoreUrl = mapUrl()

export const params = {
  f: 'json',
  where: '1=1',
  returnGeometry: 'true',
  spatialRel: 'esriSpatialRelIntersects',
  geometry:
    '{"xmin":36170.20851717582,"ymin":68408.38213096735,"xmax":36805.20978717836,"ymax":69043.38340096989,"spatialReference":{"wkt":"PROJCS["KSZB",GEOGCS["GCS_Beijing_1954",DATUM["D_Beijing_1954",SPHEROID["Krasovsky_1940",6378245.0,298.3]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",29195.5],PARAMETER["False_Northing",-3401413.0],PARAMETER["Central_Meridian",121.0],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]"}}',
  geometryType: 'esriGeometryEnvelope',
  inSR: '{"wkt":"PROJCS["KSZB",GEOGCS["GCS_Beijing_1954",DATUM["D_Beijing_1954",SPHEROID["Krasovsky_1940",6378245.0,298.3]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",29195.5],PARAMETER["False_Northing",-3401413.0],PARAMETER["Central_Meridian",121.0],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]"}',
  outFields: '*',
  outSR:
    '{"wkt":"PROJCS["KSZB",GEOGCS["GCS_Beijing_1954",DATUM["D_Beijing_1954",SPHEROID["Krasovsky_1940",6378245.0,298.3]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",29195.5],PARAMETER["False_Northing",-3401413.0],PARAMETER["Central_Meridian",121.0],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]"}',
}

//坐标原点
export let origin_ys = [-5662080.123376, 7430874.672417]
/* let url_ys = `mapApi`
axios.post(url_ys, params).then((res) => {
  const cubeData = res.data.features
  // console.log('管网数据', cubeData)

  const arr1 = []
  cubeData.forEach((value) => {
    arr1.push(value.geometry)
  })
  // console.log('一次遍历', arr1)
  const arr2 = []
  arr1.forEach((value) => {
    arr2.push(value.paths)
  })
  // console.log('二次遍历', arr2)
  const arr3 = []
  arr2.forEach((value) => {
    arr3.push(value[0])
  })
  console.log('三次遍历', arr3)
  const arr4 = []
  arr3.forEach((value) => {
    arr4.push(value[0])
  })
  // console.log('四次遍历', arr4)

  // origin_ys = [res.data.tileInfo.origin.x, res.data.tileInfo.origin.y]
}) */

proj4.defs(
  'EPSG:KSZB',
  '+proj=tmerc +lat_0=0 +lon_0=121 +k=1 +x_0=29118.5 +y_0=-3401422 +ellps=krass +units=m +no_defs +type=crs'
)
register(proj4)
export const myProjection = new Projection({
  code: 'EPSG:KSZB',
  // extent: [
  //   2662.7204187744937, 287696.0490569221, -158670.91181999963,
  //   232384.15962000092,
  // ],
  // units: 'esriMeters',
  units: 'm',
  axisOrientation: 'neu',
  global: false,
})
addProjection(myProjection)

//坐标原点
export const origin = [-5594100, 6600900]

//分辨率
export const resolutions = [
  79.37515875031751, 39.687579375158755, 21.166709000084669, 10.583354500042335,
  5.2916772500211673, 2.6458386250105836, 1.3229193125052918,
  0.52916772500211673, 0.26458386250105836, 0.13229193125052918,
  0.066145965625264591,
]

// export const resolutions = [
//   79.37515875031751, 39.687579375158755, 21.166709000084669, 10.583354500042335,
//   5.2916772500211673, 2.6458386250105836, 1.3229193125052918,
//   0.52916772500211673,
//   // 限制最大缩放级别:0.26458386250105836,0.13229193125052918, 0.066145965625264591,
// ]
//地图范围
export const fullExtent = [
  -158670.91181999957, -159300.14295692043, 232384.15962000092,
  287696.0490569221,
]
const titlGrid_ = new TileGrid({
  tileSize: 256,
  origin: origin,
  extent: fullExtent,
  resolutions: resolutions,
})
const titlGrid_ys = new TileGrid({
  tileSize: 256,
  origin: origin_ys,
  extent: fullExtent,
  resolutions: resolutions,
})
export const BaseMap = () => {
  const baseMap = new TileLayer({
    // zIndex: 9,
    source: new XYZ({
      tileGrid: titlGrid_,
      projection: myProjection,
      url: `${mapStoreUrl.BaseMapUrl}/tile/{z}/{y}/{x}`,
    }),
    type: 'overlay',
  })
  return baseMap
}

// 天地图矢量
// '+Math.round(Math.random()*7)+'
export const BaseTDT = () => {
  const BaseTDT = new Tile({
    title: 'tdtsl',
    source: new XYZ({
      crossOrigin: 'anonymous',
      url: 'http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=8d028f4ef65281c5bc14b880ac88ac66',
    }),
    type: 'overlay',
  })
  return BaseTDT
}
// 矢量注记
export const BaseTDTGJ = () => {
  const BaseTDTGJ = new Tile({
    title: 'tdtslZJ',
    source: new XYZ({
      crossOrigin: 'anonymous',
      url: 'http://t1.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=ed7c98579662348cbd6baade1472ae4e',
    }),
    type: 'overlay',
  })
  return BaseTDTGJ
}
export const ImageMap = () => {
  const ImageMap = new TileLayer({
    visible: false,
    source: new XYZ({
      tileGrid: titlGrid_,
      projection: myProjection,
      url: `${mapStoreUrl.ImageMapUrl}/tile/{z}/{y}/{x}`,
    }),
    type: 'overlay',
  })
  return ImageMap
}
// 天地图影像
export const ImgTDT = () => {
  const ImgTDT = new Tile({
    // zIndex: 1,
    title: 'tdtyx',
    source: new XYZ({
      crossOrigin: 'anonymous',
      url: 'http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=8d028f4ef65281c5bc14b880ac88ac66',
    }),
    type: 'overlay',
  })
  return ImgTDT
}
// 影像注记
export const ImgTDTGJ = () => {
  const ImgTDTGJ = new Tile({
    // zIndex: 1,
    title: 'tdtyxGJ',
    source: new XYZ({
      crossOrigin: 'anonymous',
      url: 'http://t4.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=ed7c98579662348cbd6baade1472ae4e',
    }),
    type: 'overlay',
  })
  return ImgTDTGJ
}
export const topoLayer = () => {
  const topoLayer = new Tile({
    // zIndex: 2,
    title: 'tdtdx',
    source: new XYZ({
      crossOrigin: 'anonymous',
      url: 'http://t0.tianditu.com/DataServer?T=ter_w&x={x}&y={y}&l={z}&tk=8d028f4ef65281c5bc14b880ac88ac66',
    }),
    type: 'overlay',
  })
  return topoLayer
}
export const ImgGeo = () => {
  const ImgGeo = new VectorLayer({
    zIndex: 9999,
    source: new VectorSource({
      features: new GeoJSON().readFeatures(GEOJSON_CONST),
    }),
    style: new Style({
      stroke: new Stroke({
        color: '#FF1CAE',
        width: 5,
      }),
      // fill: new Fill({
      //   color: 'rgba(255, 255, 0, 1)',
      // }),
    }),
  })
  return ImgGeo
}
export const WebMap = () => {
  const webMap = new TileLayer({
    visible: false,
    source: new XYZ({
      tileGrid: titlGrid_,
      projection: myProjection,
      url: `${mapStoreUrl.WebMapUrl}/tile/{z}/{y}/{x}`,
    }),
    type: 'overlay',
  })
  return webMap
}

export const TopographyMap = () => {
  const topographyMap = new TileLayer({
    visible: false,
    source: new XYZ({
      tileGrid: titlGrid_,
      projection: myProjection,
      url: `${mapStoreUrl.TopographyMapUrl}/tile/{z}/{y}/{x}`,
    }),
    type: 'overlay',
  })
  return topographyMap
}

export const ZT_YXGW_YS = () => {
  const zt_YXGW_YS = new TileLayer({
    visible: false,
    source: new XYZ({
      tileGrid: titlGrid_ys,
      projection: myProjection,
      url: `${mapStoreUrl.ZT_YXGW_YS_Url}/tile/{z}/{y}/{x}`,
    }),
    type: 'overlay',
  })
  return zt_YXGW_YS
}

//ZT_YXGW_YS（运行管网雨水）
//ZT_YXGW_WS（运行管网污水）
//ZT_YXGW_XQ（运行管网小区） 这三个服务是运行库的数据，需要从服务中查询其属性
/* 诊断专题 */
export const HJMap = () => {
  const HJMap = new TileLayer({
    // zIndex: 9,
    source: new XYZ({
      tileGrid: titlGrid_,
      projection: myProjection,
      // url: `${mapStoreUrl.HJUrl}/tile/{z}/{y}/{x}`,
    }),
    type: 'overlay',
  })
  return HJMap
}
